home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6752 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.7 KB  |  98 lines

  1. Path: bert.eecs.uic.edu!adesaram
  2. From: adesaram@bert.eecs.uic.edu (Asoka Desaram)
  3. Newsgroups: comp.lang.c
  4. Subject: Password changes with fork()
  5. Date: 14 Feb 1996 21:06:00 GMT
  6. Organization: University of Illinois at Chicago
  7. Message-ID: <4ftiro$kgr@news.eecs.uic.edu>
  8. NNTP-Posting-Host: bert.eecs.uic.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11.  I am trying to write a c programs using fork, exec, dup2, etc to
  12. execute passwd.  This will allow me to create a command line type of
  13. program.  This seems to be more difficult than it looks.  Does anyone
  14. have any suggestions.  Here is a copy of my code:
  15.  
  16. #include <stdio.h>                                                  
  17. #include <sys/wait.h>                                               
  18. #include <fcntl.h>                                                  
  19. #include <sys/types.h>                                              
  20. #include <unistd.h>                                                 
  21. #include <limits.h>                                                 
  22. #include <syslog.h>                                                 
  23.                                                                     
  24.                                                                     
  25. main()                                                              
  26. {                                                                   
  27.         FILE *fp,*fp1;                                              
  28.         int pipe1[2],pipe2[2],errpip[2],err_buf[64], status, pid;   
  29.         char *username, *getlogin();                                
  30.         char frombuf [BUFSIZ];                                      
  31.         char tobuf [BUFSIZ];                                        
  32.                                                                     
  33. /*                                                                  
  34.  * Create the pipe.  This has to be done                            
  35.  * Before the fork.                                                 
  36.  */                                                                 
  37.                                                                     
  38. if (pipe(pipe1) < 0) {                                              
  39.         perror("pipe");       
  40.         exit(1);              
  41. }                             
  42.                               
  43. if (pipe(pipe2) < 0) {        
  44.         perror("pipe");       
  45.         exit(1);              
  46. }                             
  47.  
  48. /*                                                               
  49.  * The child process executes the stuff inside                   
  50.  * the if.                                                       
  51.  */                                                              
  52. if (pid == 0) {                                                  
  53.                                                                  
  54.         close(pipe1[1]);                                         
  55.         close(pipe2[0]);                                         
  56.         dup2(pipe1[0],0);                                        
  57.         dup2(pipe2[1],1);                                        
  58.                                                                  
  59.         syslog(LOG_NOTICE,"In child process \n");                
  60.                                                                  
  61.         execl("/bin/passwd", "passwd", "chuae0cg", (char *) 0);  
  62.         perror("exec");                                          
  63.         exit(1);                                                 
  64. }                                                                
  65. /*                                                                 
  66.  * The parent executes this code.                                           
  67.  * write to child through pipe1[1] and write out through pipe2[0]           
  68.  */                                                                         
  69.                                                                             
  70.         close(pipe1[0]);                                                    
  71.         close(pipe2[1]);                                                    
  72.                                                                             
  73.         fp = fdopen(pipe1[1], "w");                                         
  74.         fp1 = fdopen(pipe2[0], "r");                                        
  75.                                                                             
  76.         syslog(LOG_NOTICE,"The buffer says %s \n",frombuf);                 
  77.                                                                             
  78.                                                                             
  79.         while (waitpid(pid,&status,WNOHANG) != pid)                         
  80.         {                                                                   
  81.                 /*                                                          
  82.                 * Perform interactrive I/O with child                       
  83.                 */                                                          
  84.                 read (fp1, frombuf, BUFSIZ);                                
  85.                 syslog(LOG_NOTICE,"The buffer is in waint %s \n",frombuf);  
  86.                                                                             
  87.                 /*                                                          
  88.                  * Prepare a suitable response in 'tobuf'   
  89.                  */                                         
  90.                 write (fp1, tobuf, BUFSIZ);                 
  91.                                                             
  92.         }                                                   
  93.                                                             
  94.                                                             
  95. }                                                           
  96.                                                             
  97.  
  98.